import axios from 'axios'
const KEY = '0rn8l1RiKLcgLs1mX5N0pTMKVpkNjVyU';
axios.jsonp = (url,data)=>{
    if(!url)
        throw new Error('url is necessary')
    const callback = 'CALLBACK' + Math.random().toString().substr(9,18)
    const JSONP = document.createElement('script')
          JSONP.setAttribute('type','text/javascript')

    const headEle = document.getElementsByTagName('head')[0]

    let ret = '';
    if(data){
        if(typeof data === 'string')
            ret = '&' + data;
        else if(typeof data === 'object') {
            for(let key in data)
                ret += '&' + key + '=' + encodeURIComponent(data[key]);
        }
        ret += '&_time=' + Date.now();
    }
    JSONP.src = `${url}?callback=${callback}${ret}`;
    return new Promise( (resolve) => {
        window[callback] = r => {
          resolve(r)
          headEle.removeChild(JSONP)
          delete window[callback]
        }
        headEle.appendChild(JSONP)
    })
    
}
export const getLandName = function (obj){
    return axios.jsonp('http://api.map.baidu.com/geocoder/v2/?mcode=sha1:ddd&callback=renderReverse&location='+obj.lng+','+obj.lat+'&output=json&pois=1&ak='+KEY).then(res=>res)
}